{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import quandl\n",
    "import pandas as pd\n",
    "import json\n",
    "from pymongo import MongoClient\n",
    "import datetime\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "client = MongoClient('localhost', 27017)\n",
    "db = client.futures\n",
    "market=db.market\n",
    "position=db.position\n",
    "main=db.cftc\n",
    "\n",
    "quandl.ApiConfig.api_key = '-GGCYDJNb2cxMLTvqTho'\n",
    "\n",
    "d = pd.read_excel(r'E:\\code.xlsx', input_col=0)\n",
    "ds = d[['品种名称', 'code']]\n",
    "\n",
    "\n",
    "for temp in d['code']:\n",
    "    # print(temp)\n",
    "    try:\n",
    "        data = quandl.get('CFTC/' + temp + '_F_L_ALL', paginate=True)\n",
    "        data['code'] = temp\n",
    "#         data = pd.DataFrame(data[0],columns=list(data['code']))\n",
    "#         print(data)\n",
    "        # data = quandl.get('CFTC/' + temp + '_F_L_ALL', start_date='2018-9-1', end_date='2019-09-1')\n",
    "\n",
    "        # # 净持仓\n",
    "        data['大户净持仓'] = data.apply(lambda x: x['Noncommercial Long'] - x['Noncommercial Short'], axis=1)\n",
    "#         # print(data)\n",
    "        data['套保净持仓'] = data.apply(lambda x: x['Commercial Long'] - x['Commercial Short'], axis=1)\n",
    "        data['散户净持仓'] = data.apply(lambda x: x['Nonreportable Positions Long'] - x['Nonreportable Positions Short'],\n",
    "                                   axis=1)\n",
    "        # print(data)\n",
    "        # # # 最大值最小值\n",
    "        chg = data[['大户净持仓', '套保净持仓', '散户净持仓']]\n",
    "        # print(chg)\n",
    "        max = chg.rolling(window=156).max().dropna()  # ,min_periods=1\n",
    "        min = chg.rolling(window=156).min().dropna()  # 156\n",
    "        # print(min.tail(5))\n",
    "        # # # #\n",
    "        hb = pd.merge(max, min, on=['Date'], how='outer')\n",
    "        hb1 = pd.merge(data, hb, on=['Date'], how='outer')\n",
    "        # print(hb1)\n",
    "        # # cot指标\n",
    "        data['大户cot指标(%)'] = round(\n",
    "            hb1.apply(lambda x: ((x['大户净持仓'] - x['大户净持仓_y']) / (x['大户净持仓_x'] - x['大户净持仓_y'])) * 100, axis=1), 2)\n",
    "        data['套保cot指标(%)'] = round(\n",
    "            hb1.apply(lambda x: ((x['套保净持仓'] - x['套保净持仓_y']) / (x['套保净持仓_x'] - x['套保净持仓_y'])) * 100, axis=1), 2)\n",
    "        data['散户cot指标(%)'] = round(\n",
    "            hb1.apply(lambda x: ((x['散户净持仓'] - x['散户净持仓_y']) / (x['散户净持仓_x'] - x['散户净持仓_y'])) * 100, axis=1), 2)\n",
    "\n",
    "        # data = pd.merge(data, ds, on=['code'], how='outer').dropna().drop_duplicates()\n",
    "\n",
    "        print(data.tail(10))\n",
    "    except:\n",
    "        pass\n",
    "    continue\n",
    "\n",
    "print('完成')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
